<?php $token = session('token');?>
@extends('admin.main')
@section('content')
<div class="wrapper wrapper-content">
	<div class="ibox float-e-margins">
		<div class="ibox-title">
			<h5>角色</h5>
		</div>
		<div class="ibox-content">
			@if($token['isHidden'])
			<div class="m-b">
				<a href="{{url('admin/role/add')}}" class="btn btn-warning"><i class="fa fa-plus"></i> 新建角色</a>
			</div>
			@endif
			<table id="tblDataList" class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>#</th>
						<th>名称</th>
						<th>全名</th>
						<th>描述</th>
						<th width="160">日期</th>
						<th width="260"></th>
					</tr>
				</thead>
				<tbody>
					@foreach($rows as $row)
					<tr data-id="{{$row->id}}">
						<td>{{$row->id}}</td>
						<td><span rel="name">{{$row->name}}</span></td>
						<td><span rel="fullname">{{$row->fullname}}</span></td>
						<td><span rel="description">{{$row->description}}</span></td>
						<td><span rel="created_at">{{$row->created_at}}</span></td>
						<td>
							<a href="{{url('admin/role/user-list/'.$row->id)}}" class="m-r-xs" title="查看角色用户">
								<i class="fa fa-users fa-lg"></i>成员
							</a>
							<a href="{{url('admin/role/permissions-list/'.$row->id)}}" class="m-r-xs" title="查看角色权限">
								<i class="fa fa-unlock-alt fa-lg"></i>权限
							</a>
							<a href="javascript:;" class="m-r-xs js-edit">
								<i class="fa fa-pencil-square fa-lg"></i>编辑
							</a>
							@if($token['isHidden'])
							<a href="javascript:;" class="m-r-xs js-del">
								<i class="fa fa-times-circle fa-lg"></i>删除
							</a>
							@endif
						</td>
					</tr>
					@endforeach
				</tbody>
			</table>
		</div>
	</div>
</div>

<script id="tplEditPanel" type="text/html">
	<form id="formRoleEdit" method="post" class="form-horizontal" role="form">
		<div class="form-group">
			<label class="col-sm-2 control-label">名称</label>
			<div class="col-sm-4">
				<p class="form-control-static"><%name%></p>
			</div>
		</div>
		<div class="hr-line-dashed"></div>
		<div class="form-group">
			<label class="col-sm-2 control-label">全名</label>
			<div class="col-sm-6">
				<input type="text" name="fullname" value="<%fullname%>" class="form-control" required="">
			</div>
		</div>
		<div class="hr-line-dashed"></div>
		<div class="form-group">
			<label class="col-sm-2 control-label">描述</label>
			<div class="col-sm-10">
				<input type="text" name="description" value="<%description%>" class="form-control" required="">
			</div>
		</div>
		<div class="hr-line-dashed"></div>
		<div class="form-group">
			<div class="col-sm-4 col-sm-offset-2">
				<input type="hidden" name="id" value="<%id%>">
				<button class="btn btn-primary btn-w-m" type="submit">提交</button>
			</div>
		</div>
	</form>
</script>
@endsection

@section('pagescript')
<script src="{{asset('assets/models/roleModel.js')}}"></script>
<script>
	var artDialog;
	$("#sidebar li[rel='entrust']").addClass("active")
		.find("ul").addClass("in")
		.find("li[rel='1']").addClass("active");

	$("#tblDataList").delegate('.js-edit', 'click', function(){
		var tr = $(this).parents('tr').eq(0);
		var data = {'id':tr.data('id')};
		$(tr).find("span[rel]").each(function(){
			data[$(this).attr('rel')] = $(this).html();
		});
		artDialog = dialog({
			title: '编辑',
			content: template('tplEditPanel', data),
			width: '600px',
			onshow: function(){
				$('#formRoleEdit').on('submit', function(){
					var formdata = $(this).serialize();
					roleModel.update(formdata, function(result){
						$(tr).find("span[rel]").each(function(){
							$(this).html(result[$(this).attr('rel')]);
						});
						artDialog.close().remove();
					}, failure);
					return false;
				});
			}
		}).showModal();
	});

	$("#tblDataList").delegate('.js-del', 'click', function(e){
		var id = $(this).parents('tr').eq(0).data('id');
		dialog({
			content:'<i class="fa fa-info-circle"></i> 确定要删除此角色吗？',
			ok:function(){
				roleModel.delete({'id':id}, function(){
					$(e.target).parents("tr").remove();
				}, failure);
			},
			cancel: true
		}).showModal();
	});
</script>
@endsection